package com.fusion.core.comm.rpc;

import com.fusion.core.comm.Packet;
import com.fusion.core.interactions.PostMessageInteraction;
import com.fusion.core.interactions.PostMessageToAllInteraction;

import java.io.IOException;

/**
 *
 * version 2 of chat message
 *
 * @author SwethaShivakumar 
 * Project: fusion
 * Date: Mar 9, 2009
 * URL: http://fusionwiki.co.cc
 * Mail Me At: shiva@fusionproject.co.cc
 */
public class ChatMessageV3 extends PersistantRPC {

	private static final long serialVersionUID = 1L;
	private String message;
    private boolean messageToAll;
    private long sentAtTick;

    public ChatMessageV3() {
        routable = true;
    }

    public ChatMessageV3(String message, boolean messageToAll) {
        this.message = message;
        this.messageToAll = messageToAll;
        sentAtTick = System.currentTimeMillis();
    }

    public void execute(Packet in) throws IOException {
        message = in.readUTF();
        messageToAll = in.readBoolean();
        sentAtTick = in.readLong();
        hasBeenQueuedForLaterSend = in.readBoolean();
        if (messageToAll)
            manager.getCore().queNeedsUserInteraction(new PostMessageToAllInteraction(message, fromGuid, sentAtTick, hasBeenQueuedForLaterSend));
        else
            manager.getCore().queNeedsUserInteraction(new PostMessageInteraction(message, fromGuid, sentAtTick, hasBeenQueuedForLaterSend));
    }

    public Packet serializeTo(Packet p) {
        p.writeUTF(message);
        p.writeBoolean(messageToAll);
        p.writeLong(sentAtTick);
        p.writeBoolean(hasBeenQueuedForLaterSend);
        return p;
    }
}
